﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VSSAHT.DataAccess;
using VSSAHT.Transfer;
namespace VSS_AHT
{
    public partial class FormUpdateModelVehicle : BaseForm
    {
        #region Ctors
        public FormUpdateModelVehicle()
        {
            InitializeComponent();
            Initialize();
        }
        public void Initialize()
        {
            this.MaximizeBox = false;

            this.Load += new EventHandler(delegate
            {
                BindingControls();
                ValidatingControls();

               // this.txtModelVehicleCode.Enabled = !_isEdit;
            });
            this.btnSave.Click += new EventHandler(delegate { ClickButtonSave(); });
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
        }

        #endregion


        #region Property Member
        private bool _isEdit = false;
        private bool _isBinding = false;
        private ModelVehicleDTO _modelvehicle = new ModelVehicleDTO();
        ModelVehicleDataAccess _dataAccess = new ModelVehicleDataAccess();
        public ModelVehicleDTO ModelVehicle
        {
            get { return _modelvehicle; }
            set
            {
                if (_modelvehicle != value)
                {
                    _isEdit = true;
                    _modelvehicle = value;
                }
            }
        }

        private IList<ModelVehicleDTO> _collection;
        public IList<ModelVehicleDTO> Collection
        {
            set { _collection = value; }
        }
        #endregion

        #region Methods
        private void BindingControls()
        {
            if (!_isBinding)
            {
                this.txtModelVehicleCode.DataBindings.Add("Text", ModelVehicle, "ModelVehicleNumber");
                this.txtModelVehicleName.DataBindings.Add("Text", ModelVehicle, "ModelVehicleWeight");
               
               
                _isBinding = true;
            }
        }

        private void ValidatingControls()
        {
            string errorMessage = String.Empty;
            this.txtModelVehicleCode.Validating += new CancelEventHandler(delegate
            {
                errorMessage = String.IsNullOrEmpty(this.txtModelVehicleCode.Text) ?
                    "Số xe cần được nhập" : String.Empty;
                errorProvider.SetError(this.txtModelVehicleCode, errorMessage);
            });
            this.txtModelVehicleName.Validating += new CancelEventHandler(delegate
            {
                errorMessage = String.IsNullOrEmpty(this.txtModelVehicleName.Text) ?
                    "Trọng lượng xe cần được nhập" : String.Empty;
                errorProvider.SetError(this.txtModelVehicleName, errorMessage);
            });
        }

        private void ResetBindingControls()
        {
            this.txtModelVehicleCode.DataBindings.Clear();
            this.txtModelVehicleName.DataBindings.Clear();
           
            this._isBinding = false;
        }

        private void ClickButtonSave()
        {
            bool invalidInput = false;

            foreach (Control ctrl in this.Controls)
            {
                if (errorProvider.GetError(ctrl) != String.Empty)
                {
                    invalidInput = true;
                    break;
                }
            }

            if (invalidInput)
            {
                Common.ShowAlert("Thông tin bạn nhập chưa hợp lệ, vui lòng kiểm tra lại");
            }
            else
            {
                this.errorProvider.Clear();

                bool result = false;
                if (!_isEdit)
                {
                    result = _dataAccess.Add(_modelvehicle);

                    if (result)
                    {
                        _collection.Add(_modelvehicle);
                        _modelvehicle = new ModelVehicleDTO();
                        ResetBindingControls();
                        BindingControls();
                        this.txtModelVehicleCode.Focus();
                    }
                    else
                    {
                        Common.ShowAlert("Tạo mới thông tin trọng lượng mẫu không thành công!");
                    }
                }
                else
                {
                    result = _dataAccess.Edit(_modelvehicle);

                    if (result)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        Common.ShowAlert("Chỉnh sửa thông tin trọng lượng mẫu không thành công!");
                    }

                }
            }
        }
        #endregion
    }
}